
import { loginByUsername } from '../../api/index'
import { getToken, setToken, removeToken } from '../../utils/auth'
const user = {
    state: {
        user: '',
        token: getToken(),
        name: '',
        avatar: '',
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_PERMS: (state, perms) => {
            state.perms = perms
        }
    },

    actions: {
        // 用户名登录
        LoginByUsername({ commit }, userInfo) {
            console.log(36, userInfo)
            console.log(37, { commit })

            return new Promise((resolve, reject) => {
                loginByUsername(userInfo).then(response => {
                    console.log(420, response);
                    const token = response.data.data.token
                    const avatar = response.data.data.adminInfo.avatar
                    const name = response.data.data.adminInfo.nickname
                    commit('SET_AVATAR', avatar)
                    commit('SET_TOKEN', token)
                    commit('SET_NAME', name)
                    setToken(token)
                    resolve(response)
                }).catch(response => {
                    if (response.status && response.status === 200) {
                        console.log(41, userInfo);
                        console.log(42, response);
                        const token = response.data.data.token
                        commit('SET_TOKEN', token)
                        setToken(token)
                        resolve(response)
                        return
                    }
                    reject(response)
                })
            })
        },


    }
}

export default user
